package br.gendi.core;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Conectar {

	public static void main(String a[]) throws Exception {
		netmhc();
	}

	public static void executarGet() {
		//http://www.jenner.ac.uk/scripts/additive3in1.pl?seq=mattyeefaa+kldrldeefn+kkmqeqnakf+fadkpdestl+spemkehydk+fermikehte+kfnkkmhehs+ehfkhkfael+leqqkaaqyp+sk&dna=++++++++&orf=3&HLA=A0101&tcut=3&output=0.05&submit=Submit
		HttpClient client = new HttpClient();
		client.getParams().setParameter("http.useragent", "Test Client");

		BufferedReader br = null;

		GetMethod method = new GetMethod("http://www.jenner.ac.uk/scripts/additive3in1.pl");

		method.setFollowRedirects(true);

		NameValuePair[] params = {
				new NameValuePair("seq", "mattyeefaa+kldrldeefn+kkmqeqnakf+fadkpdestl+spemkehydk+fermikehte+kfnkkmhehs+ehfkhkfael+leqqkaaqyp+sk"),
				new NameValuePair("dna", "++++++++"),
				new NameValuePair("orf","3"),
				new NameValuePair("HLA","A0101"),
				new NameValuePair("tcut","3"),
				new NameValuePair("output", "0.05"),
				new NameValuePair("submit","Submit"),

		};
		method.setQueryString(params);

		try {
			int returnCode = client.executeMethod(method);

			if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
				System.err.println("The Post method is not implemented by this URI");
				// still consume the response body
				method.getResponseBodyAsString();
			} else {
				br = new BufferedReader(new InputStreamReader(method
						.getResponseBodyAsStream()));
				String readLine;
				while (((readLine = br.readLine()) != null)) {
					System.err.println(readLine);
				}
			}
		} catch (Exception e) {
			System.err.println(e);
		} finally {
			method.releaseConnection();
			if (br != null)
				try {
					br.close();
				} catch (Exception fe) {
				}
		}

	}

	public static void netmhc() {
		//http://www.jenner.ac.uk/scripts/additive3in1.pl?seq=mattyeefaa+kldrldeefn+kkmqeqnakf+fadkpdestl+spemkehydk+fermikehte+kfnkkmhehs+ehfkhkfael+leqqkaaqyp+sk&dna=++++++++&orf=3&HLA=A0101&tcut=3&output=0.05&submit=Submit
		HttpClient client = new HttpClient();
		client.getParams().setParameter("http.useragent", "Test Client");

		BufferedReader br = null;

		GetMethod method = new GetMethod("http://www.cbs.dtu.dk/cgi-bin/webface");

		method.setFollowRedirects(true);

		NameValuePair[] params = {
				new NameValuePair("configfile", "/usr/opt/www/pub/CBS/services/NetMHC-3.0/NetMHC.cf"),
				new NameValuePair("SEQPASTE", "mattyeefaa+kldrldeefn+kkmqeqnakf+fadkpdestl+spemkehydk+fermikehte+kfnkkmhehs+ehfkhkfael+leqqkaaqyp+sk"),
				new NameValuePair("peplen", "9"),
				new NameValuePair("alleles", "A0101")			
		};
		method.setQueryString(params);

		try {
			int returnCode = client.executeMethod(method);
			
			if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
				System.err.println("The Post method is not implemented by this URI");
				// still consume the response body
				method.getResponseBodyAsString();
			} else {
				br = new BufferedReader(new InputStreamReader(method
						.getResponseBodyAsStream()));
				String readLine;
				while (((readLine = br.readLine()) != null)) {
					System.err.println(readLine);
				}
			}
		} catch (Exception e) {
			System.err.println(e);
		} finally {
			method.releaseConnection();
			if (br != null)
				try {
					br.close();
				} catch (Exception fe) {
				}
		}

	}

	public void executarPost() {

		HttpClient client = new HttpClient();
		client.getParams().setParameter("http.useragent", "Test Client");

		BufferedReader br = null;

		PostMethod method = new PostMethod("http://search.yahoo.com/search");
		method.addParameter("p", "\"java2s\"");

		try {
			int returnCode = client.executeMethod(method);

			if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
				System.err.println("The Post method is not implemented by this URI");
				// still consume the response body
				method.getResponseBodyAsString();
			} else {
				br = new BufferedReader(new InputStreamReader(method
						.getResponseBodyAsStream()));
				String readLine;
				while (((readLine = br.readLine()) != null)) {
					System.err.println(readLine);
				}
			}
		} catch (Exception e) {
			System.err.println(e);
		} finally {
			method.releaseConnection();
			if (br != null)
				try {
					br.close();
				} catch (Exception fe) {
				}
		}

	}
}
